package net.toocruel.iqismart.service.core.impl;

import net.toocruel.iqismart.entity.setting.Setting;
import net.toocruel.iqismart.service.core.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/13 下午11:37
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public void send(String sendTo, String title, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(Setting.SETTING.getEmail().getFromName()+" <"+Setting.SETTING.getEmail().getFromAddress()+">");
        message.setTo(sendTo);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);

    }

    @Override
    public void sendHtml(String sendTo,String title, String html) throws MessagingException {
        //建立邮件消息,发送简单邮件和html邮件的区别
        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

        //设置收件人，寄件人
        messageHelper.setTo(sendTo);
        messageHelper.setFrom(Setting.SETTING.getEmail().getFromName()+" <"+Setting.SETTING.getEmail().getFromAddress()+">");
        messageHelper.setSubject(title);
        //true 表示启动HTML格式的邮件
        messageHelper.setText(html,true);

        //发送邮件
        mailSender.send(mailMessage);

    }
}
